package com.lynn.salary.util;

import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;

public class DBUtil {

	private static BasicDataSource dataSource = null;
	static {
		try {
			Properties prop = new Properties();
			InputStream is = DBUtil.class.getResourceAsStream("/jdbc.properties");
			prop.load(is );
			dataSource = (BasicDataSource) BasicDataSourceFactory
					.createDataSource(prop);
			is.close();
		} catch (Exception e) {
			System.err.println("DBUtil::static 创建数据源失败");
			e.printStackTrace();
		}

	}

	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			System.err.println("获取数据库连接异常");
		}
		return null;
	}

	public static int getNumActivity() {
		return dataSource.getNumActive();
	}

	public static int getNumMaxActive() {
		return dataSource.getMaxActive();
	}
	
	public static void destory() throws SQLException {
		dataSource.close();
		System.out.println("\t\tdestory DBUtil completed ... ");
	}

//	 public static void main(String[] args) throws Exception {
//		 Connection conn = DBUtil.getConnection();
//		 conn.close();
//		 conn = DBUtil.getConnection();
//		 conn.close();
//		 conn = DBUtil.getConnection();
//		 Statement stat = conn.createStatement();
//		 ResultSet r = stat.executeQuery("select * from employee");
//		 ResultSetMetaData rsmd = r.getMetaData();
//		 while (r.next()) {
//		 for (int i = 1; i < rsmd.getColumnCount(); i++) {
//		 System.out.println(rsmd.getColumnLabel(i) + ":"
//		 + r.getObject(i));
//		 }
//		 }
//		
//		 System.out.println(DBUtil.getNumActivity());
//		 System.out.println(DBUtil.getNumMaxActive());
//	 }
}
